package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"yk-travel-social/client/client"
	"yk-travel-social/client/enit/request"
	"yk-travel-social/client/enit/response"
	"yk-travel-social/common/proto/travel"
)

func Login(c *gin.Context) {
	var data request.Login

	err := c.ShouldBind(&data)
	if err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.Login(c, &travel.LoginRequest{
		Mobile:   data.Mobile,
		Password: data.Password,
	})
	if err != nil {
		response.ErrorResponse(c, "登入失败", err.Error())
		return
	}

	response.SuccessResponse(c, "登入成功", login)
}

func Search(c *gin.Context) {
	var data request.Search

	err := c.ShouldBind(&data)
	if err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.Search(c, &travel.SearchRequest{
		ScenicSpotName: data.ScenicSpotName,
	})
	if err != nil {
		response.ErrorResponse(c, "搜索失败", err.Error())
		return
	}

	response.SuccessResponse(c, "搜索成功", login)
}

func ShowInfo(c *gin.Context) {
	var data request.ShowInfo

	err := c.ShouldBind(&data)
	if err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.ShowInfo(c, &travel.ShowInfoRequest{
		ScenicSpotId: int64(data.ScenicSpotId),
	})
	if err != nil {
		response.ErrorResponse(c, "详情展示失败", err.Error())
		return
	}

	response.SuccessResponse(c, "详情展示成功", login)
}

func BuyTicket(c *gin.Context) {
	var data request.BuyTicket

	err := c.ShouldBind(&data)
	if err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	userId := c.GetUint("userId")

	login, err := client.BuyTicket(c, &travel.BuyTicketRequest{
		UserId:       int64(userId),
		ScenicSpotId: int64(data.ScenicSpotId),
		PurchaserId:  int64(data.PurchaserId),
		Num:          int64(data.Num),
	})
	if err != nil {
		response.ErrorResponse(c, "抢购失败", err.Error())
		return
	}

	response.SuccessResponse(c, "抢购成功", login)
}

func AddPurchaser(c *gin.Context) {
	var data request.AddPurchaser

	err := c.ShouldBind(&data)
	if err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.AddPurchaser(c, &travel.AddPurchaserRequest{
		Name:   data.Name,
		IdCary: data.IdCarys,
	})
	if err != nil {
		response.ErrorResponse(c, "添加失败", err.Error())
		return
	}

	response.SuccessResponse(c, "添加成功", login)
}

func AlipayBack(c *gin.Context) {
	status := c.PostForm("trade_status")
	sn := c.PostForm("out_trade_no")

	if status == "TRADE_FINISHED" {
		status = "1"
	}

	if status == "TRADE_SUCCESS" {
		status = "2"
	}

	if status == "WAIT_BUYER_PAY" {
		status = "3"
	}

	if status == "TRADE_CLOSED" {
		status = "4"
	}
	fmt.Println(status)

	client.AlipayBack(c, &travel.AlipayBackRequest{
		Sn:     sn,
		Status: status,
	})
}

func ShowOrder(c *gin.Context) {
	userId := c.GetUint("userId")

	login, err := client.ShowOrder(c, &travel.ShowOrderRequest{
		UserId: int64(userId),
	})
	if err != nil {
		response.ErrorResponse(c, "查询失败", err.Error())
		return
	}

	response.SuccessResponse(c, "查询成功", login)
}
